/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project.facades;

import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import project.dto.NewFeedDTO;
import project.entities.NewFeedEntity;
import project.facades.exceptions.NonexistentEntityException;
import project.facades.interfaces.NewFeedManager;

/**
 *
 * @author armen.arzumanyan@gmail.com
 */
@ManagedBean(name = "NewFeedFacade")
@SessionScoped
public class NewFeedFacade implements  NewFeedManager{

    public NewFeedFacade() {
        emf = Persistence.createEntityManagerFactory("cmsPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }



      public List<NewFeedDTO> getFeedList(Integer start, Integer count) {
        List<NewFeedDTO> userList = new Vector<NewFeedDTO>();
        try {
            EntityManager em = getEntityManager();
            Query query = em.createQuery("SELECT c.id,  c.url, c.subject, c.content, c.media, c.datePulished "
                    + " FROM NewFeedEntity c ORDER BY c.datePulished DESC");

            if (start != null && start >= 0) {
                query.setFirstResult(start);
            }
            if (count != null && count > 0) {
                query.setMaxResults(count);
            }

            List<Object[]> list = query.getResultList();

            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    Object[] result = list.get(i);
                    NewFeedDTO users = new NewFeedDTO();
                    users.setId((Long) result[0]);
                    users.setUrl((String) result[1]);
                    users.setSubject((String) result[2]);
                    users.setContent((String) result[3]);
                    users.setMedia((String) result[4]);
                    users.setDatePulished((Date) result[5]);                   
                    userList.add(users);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    public void create(NewFeedEntity newFeedEntity) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(newFeedEntity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(NewFeedEntity newFeedEntity) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            newFeedEntity = em.merge(newFeedEntity);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = newFeedEntity.getId();
                if (findNewFeedEntity(id) == null) {
                    throw new NonexistentEntityException("The newFeedEntity with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            NewFeedEntity newFeedEntity;
            try {
                newFeedEntity = em.getReference(NewFeedEntity.class, id);
                newFeedEntity.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The newFeedEntity with id " + id + " no longer exists.", enfe);
            }
            em.remove(newFeedEntity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<NewFeedEntity> findNewFeedEntityEntities() {
        return findNewFeedEntityEntities(true, -1, -1);
    }

    public List<NewFeedEntity> findNewFeedEntityEntities(int maxResults, int firstResult) {
        return findNewFeedEntityEntities(false, maxResults, firstResult);
    }

    private List<NewFeedEntity> findNewFeedEntityEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from NewFeedEntity as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public NewFeedEntity findNewFeedEntity(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(NewFeedEntity.class, id);
        } finally {
            em.close();
        }
    }

    public int getNewFeedEntityCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from NewFeedEntity as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
